import { createApp } from 'vue';
import App from '@/components/App.vue'; // 确保路径正确
import router from './router';
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
import Web3 from 'web3';

// 创建 Vue 实例
const app = createApp(App);

// 注册 Element Plus 图标组件
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

app.use(router).mount('#app');

// 如果用户安装了 MetaMask，我们会初始化 Web3
if (window.ethereum) {
  const web3 = new Web3(window.ethereum);

  // 尝试连接钱包
  const connectWallet = async () => {
    try {
      // 请求连接钱包
      const accounts = await window.ethereum.request({
        method: 'eth_requestAccounts',
      });

      if (accounts.length > 0) {
        console.log('Connected account:', accounts[0]);
      }
    } catch (error) {
      console.error('Error connecting to wallet:', error);
    }
  };

  // 监听账户变化
  window.ethereum.on('accountsChanged', (accounts: string[]) => {
    console.log('Account changed:', accounts[0]);
  });

  // 监听网络变化
  window.ethereum.on('chainChanged', (chainId: string) => {
    console.log('Network changed:', chainId);
  });

  // 调用连接钱包函数
  connectWallet();
} else {
  console.log('MetaMask is not installed');
}
